package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 锦州在线
 * @author Persh
 *
 */
public class JzpttNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.jzptt.ln.cn/jzptthtm/newspages2.php\\?Newsid=[0-9]*",
		"http://www.jzptt.ln.cn/jkzx/news[0-9]*.php\\?Newsid=[0-9]*"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div[align=center] font[size=4]").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "|");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("td.STYLE2"));

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div[align=center]").text();
		author=StringUtils.substringBetween(authorStr, "来源:", "有");

		date=DateUtils.matchDate(doc.select("div[align=center]").text());

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		JzpttNewsAnalyse analyse=new JzpttNewsAnalyse();
		String url="http://www.jzptt.ln.cn/jkzx/news10.php?Newsid=1100";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
